//
//  UIKitFactory.m
//  DSDeveloperKit
//
//  Created by FengDongsheng on 16/1/20.
//  Copyright © 2016年 FengDongsheng. All rights reserved.
//

#import "UIKitFactory.h"

@implementation UIKitFactory

/**
 *  生成UILabel
 *
 *  @param frame          位置
 *  @param text           文字
 *  @param font           字体大小
 *  @param superView      父试图
 *
 *  @return UILabel
 */
+ (UILabel *)szb_labelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)alignment SuperView:(UIView *)superView{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = alignment;
    [superView addSubview:label];
   
    return label;
}




/**
 生成szb_button
 
 @param frame 位置
 @param superView 父视图
 @param imageName 图片名
 @param backImageName 背景图片名字
 @param font 字体
 @param titleColor 字体颜色
 @param backGroundColor 视图背景颜色
 @param title 标题
 @param target 事件
 @param action 行为
 @return UIButton
 */
+ (UIButton *)szb_buttonWithFrame:(CGRect)frame superView:(nullable UIView *)superView setImage:(nullable NSString *)imageName setBackImage:(nullable NSString *)backImageName font:(nullable UIFont *)font titleColor:(nullable UIColor *)titleColor backgroundColor:(nullable UIColor *)backGroundColor  title:(nullable NSString *)title target:(nullable id)target action:(nullable SEL)action {
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = backGroundColor;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [superView addSubview:btn];
    
    return btn;
    
}


/**
 生成一个带图片，无文字的button

 @param frame frame description
 @param superView superView description
 @param imageName imageName description
 @param backImageName backImageName description
 @param target target description
 @param action action description
 @return return value description
 */
+ (UIButton *)szb_imageButtonWithFrame:(CGRect)frame superView:(nullable UIView *)superView setImage:(nullable NSString *)imageName setBackImage:(nullable NSString *)backImageName target:(nullable id)target action:(nullable SEL)action {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [superView addSubview:btn];
    
    return btn;
    
}


/**
 生成一个无图片，有文字的button

 @param frame frame description
 @param superView superView description
 @param font font description
 @param titleColor titleColor description
 @param backGroundColor backGroundColor description
 @param title title description
 @param target target description
 @param action action description
 @return return value description
 */
+ (UIButton *)szb_titleButtonWithFrame:(CGRect)frame superView:(nullable UIView *)superView font:(nullable UIFont *)font titleColor:(nullable UIColor *)titleColor backgroundColor:(nullable UIColor *)backGroundColor  title:(nullable NSString *)title target:(nullable id)target action:(nullable SEL)action {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = backGroundColor;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [superView addSubview:btn];
    
    return btn;
    
}

/**
 *  生成UIView
 *
 *  @param frame          位置
 *  @param bgColor        背景颜色
 *  @param superView      父试图
 *
 *  @return UIView
 */

+ (UIView *)szb_viewWithFrame:(CGRect)frame backgroundColor:(nullable UIColor *)bgColor superView:(UIView *)superView{
    UIView *myView = [[UIView alloc]initWithFrame:frame];
    myView.backgroundColor = bgColor;
    [superView addSubview:myView];
    return  myView;
}

/**
 *  生成UIImageView
 *
 *  @param frame          位置
 *  @param imageName      图片名字
 *  @param superView      父试图
 *
 *  @return UIImageView
 */
+ (UIImageView *)szb_imageViewWithFrame:(CGRect)frame imageName:(NSString *)imageName superView:(UIView *)superView{
    UIImageView *imv = [[UIImageView alloc]initWithFrame:frame];
    [imv setImage:[UIImage imageNamed:imageName]];
    [superView addSubview:imv];
    return imv;
}





/**
 *  画虚线
 *
 *  @param Width           线宽
 *  @param lineColor       线条颜色
 *  @param originX         起始位置x
 *  @param originY         起始位置y
 *  @param lineBreakWidth  虚线每段宽度
 *  @param lineBreakMargin 虚线之间的间距
 *  @param superView       父试图
 *
 *  @return UITextField
 */
+ (CAShapeLayer*)shapDottedLineWidth:(CGFloat)Width lineColor:(UIColor *)lineColor  lineBreakWidth:(int)lineBreakWidth lineBreakMargin:(int)lineBreakMargin originX:(CGFloat)originX originY:(CGFloat)originY superView:(UIView *)superView{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:superView.bounds];
    [shapeLayer setPosition:superView.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [shapeLayer setStrokeColor:lineColor.CGColor];
    
    // 3.0f设置虚线的宽度
    [shapeLayer setLineWidth:Width];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:lineBreakWidth],
      [NSNumber numberWithInt:lineBreakMargin],nil]];
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, originX, originY);
    CGPathAddLineToPoint(path, NULL, SCREEN_WIDTH,originY);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
    [[superView layer] addSublayer:shapeLayer];
    
    return shapeLayer;
}

/**
 *  画虚线
 *
 *  @param Height          线高
 *  @param lineColor       线条颜色
 *  @param originX         起始位置x
 *  @param originY         起始位置y
 *  @param lineWidth       虚线的宽度
 *  @param lineBreakWidth  虚线每段宽度
 *  @param lineBreakMargin 虚线之间的间距
 *  @param superView       父试图
 *
 *  @return CAShapeLayer
 */
+ (CAShapeLayer*)shapDottedLineHeight:(CGFloat)Height lineColor:(UIColor *)lineColor  lineBreakWidth:(int)lineBreakWidth lineBreakMargin:(int)lineBreakMargin originX:(CGFloat)originX originY:(CGFloat)originY lineWidth:(CGFloat)lineWidth superView:(UIView *)superView{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:superView.bounds];
    [shapeLayer setPosition:superView.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [shapeLayer setStrokeColor:lineColor.CGColor];
    
    // 3.0f设置虚线的宽度
    [shapeLayer setLineWidth:Height];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:lineBreakWidth],
      [NSNumber numberWithInt:lineBreakMargin],nil]];
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, originX, originY);
    CGPathAddLineToPoint(path, NULL, originX + lineWidth,originY);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
    [[superView layer] addSublayer:shapeLayer];
    
    return shapeLayer;
    
}


+ (UITextField *_Nullable)szb_textFieldWithFrame:(CGRect)frame WithPlaceholder:(NSString *_Nullable)placeholder withDelegat:(id _Nullable )delegate superView:(UIView *_Nullable)superView textAlignment:(NSTextAlignment)textAlignment textFont:(UIFont *_Nullable)font textColol:(UIColor *_Nullable)color{
    UITextField * tf = [[UITextField alloc] initWithFrame:frame];
    tf.placeholder = placeholder;
    tf.font = font;
    tf.delegate = delegate;
    tf.clearsOnBeginEditing = YES;
    tf.textAlignment = textAlignment;
    tf.font = font;
    tf.textColor = color;
    [superView addSubview:tf];
    return tf;
}

+ (UIView *)viewWithImg:(UIImage *)img {
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 18)];
    UIImageView * vImg = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 10, 21)];
    vImg.image =img;
    [view addSubview:vImg];
    return view;
}


/**
 设置圆角

 @param rect rect description
 @param radius radius description
 @return return CAShapeLayer description
 */
+ (CAShapeLayer *)roundedRect:(CGRect)rect cornerRadius:(CGFloat)radius{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
    //            [UIBezierPath bezierPathWithRoundedRect:self.typeLab1.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.typeLab1.bounds.size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = rect;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    return maskLayer;
}

/**
 设置选择性设置圆角

 @param rect rect description
 @param corners corners description
 @param cornerRadii radius description
 @return return value description
 */
+ (CAShapeLayer *)roundedRect:(CGRect)rect roundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = rect;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    return maskLayer;
}


#pragma mark - 设置UITableViewStylePlain 时候控制区头不滚动
/**
 设置UITableViewStylePlain 时候控制区头不滚动
 
 @param tableview tableview
 @param sectionHeaderHeight 区头高
 @param sectionFooterHeight 区尾稿
 */
+ (void)configTableViewStylePlainWhenSectionheardViewNotScrolleWithTabView:(UITableView *)tableview
                                                       SectionHeaderHeight:(CGFloat)sectionHeaderHeight
                                                       sectionFooterHeight:(CGFloat)sectionFooterHeight{
    
    CGFloat offsetY = tableview.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
    } else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
    {
        tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
    } else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)
    {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
    }
}


+ (void)configFooterViewWithTabView:(UITableView *)tableView pages:(NSInteger )pages pageNum:(NSInteger)pageNum count:(NSInteger)count cellHeight:(CGFloat)cellHeight headerViewHeight:(CGFloat)headerViewHeight{
    
    if (pages == pageNum || pages == 0) {
        CGFloat footViewHeight = 0.0;
        NSLog(@"%f",CGRectGetHeight(tableView.frame));
        if (pages>1) {
            footViewHeight = 126*AutoSizeScaleYY;
        }else{
            if (cellHeight *(count+2) +headerViewHeight> CGRectGetHeight(tableView.frame)) {
                footViewHeight = 126*AutoSizeScaleYY;
            }else{
                footViewHeight = CGRectGetHeight(tableView.frame) - cellHeight *count - headerViewHeight;
            }
        }
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, footViewHeight)];
        footView.backgroundColor = [UIColor whiteColor];
        UILabel *warnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(footView.frame)-15-14, SCREEN_WIDTH, 14)];
        warnLabel.text = @"我是有底线的";
        warnLabel.textAlignment = NSTextAlignmentCenter;
        warnLabel.textColor = [@"#9b9b9b" hexStringToColor];
        warnLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
        [footView addSubview:warnLabel];
        tableView.tableFooterView = footView;
        tableView.mj_footer.hidden = YES;
    }else{
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 8*AutoSizeScaleXX)];
        tableView.mj_footer.hidden = NO;
    }
}

+ (void)configFooterViewWithTabView:(UITableView *)tableView pages:(NSInteger )pages pageNum:(NSInteger)pageNum count:(NSInteger)count cellHeight:(CGFloat)cellHeight headerViewHeight:(CGFloat)headerViewHeight title:(NSString *)title{
    
    if (pages == pageNum || pages == 0) {
        CGFloat footViewHeight = 0.0;
        NSLog(@"%f",CGRectGetHeight(tableView.frame));
        if (pages>1) {
            footViewHeight = 126*AutoSizeScaleYY;
        }else{
            if (cellHeight *(count+2) +headerViewHeight> CGRectGetHeight(tableView.frame)) {
                footViewHeight = 126*AutoSizeScaleYY;
            }else{
                footViewHeight = CGRectGetHeight(tableView.frame) - cellHeight *count - headerViewHeight;
            }
        }
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, footViewHeight)];
        footView.backgroundColor = [UIColor whiteColor];
        UILabel *warnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(footView.frame)-15-14, SCREEN_WIDTH, 14)];
        warnLabel.text = title;
        warnLabel.textAlignment = NSTextAlignmentCenter;
        warnLabel.textColor = [UIColor grayColor];
        warnLabel.font = [UIFont systemFontOfSize:12];
        [footView addSubview:warnLabel];
        tableView.tableFooterView = footView;
        tableView.mj_footer.hidden = YES;
    }else{
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 8*AutoSizeScaleXX)];
        tableView.mj_footer.hidden = NO;
    }
}



//设置渐变色
+ (void )gradientLayerWithSuperView:(UIView *)superView starColor:(UIColor *)starColor  endColor:(UIColor *)endColor startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint locations:(NSArray *)locations{
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)starColor.CGColor, (__bridge id)endColor.CGColor];
    // 各个阶梯的区间百分比
    gradientLayer.locations = locations;
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    gradientLayer.frame = superView.frame;
    [superView.layer addSublayer:gradientLayer];
    
    
}

//设置渐变色
+ (void )gradientLayerFromLeftToRightWithSuperView:(UIView *)superView starColor:(UIColor *)starColor endColor:(UIColor *)endColor{
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)starColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = superView.frame;
    [superView.layer addSublayer:gradientLayer];
    
    
}

/**
 配置视图阴影
 
 @param shadowView 配置阴影的视图
 @param color 阴影颜色
 @param opacity 阴影透明度
 @param offset 阴影的范围
 @param radius 阴影扩散的范围控制
 */
+ (void)shadowLayerWithView:(UIView *_Nullable)shadowView
                shadowColor:(UIColor *_Nullable)color
              shadowOpacity:(CGFloat)opacity
               shadowOffset:(CGSize)offset
               shadowRadius:(CGFloat)radius{
    
    //设置控件的阴影
    shadowView.layer.shadowPath =[UIBezierPath bezierPathWithRect:shadowView.layer.bounds].CGPath;
    shadowView.layer.shadowColor = color.CGColor;//阴影的颜色
    shadowView.layer.shadowOpacity =opacity;   // 阴影透明度
    shadowView.layer.shadowOffset =offset; // 阴影的范围
    shadowView.layer.shadowRadius =radius;  // 阴影扩散的范围控制
}

+ (void)attributedText:(UILabel *)label font:(id)font range:(NSRange)range color:(UIColor *)color
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    label.attributedText = str;
}

+ (CGSize)attribut:(UILabel *)label{
    return [label.text sizeWithAttributes:@{NSFontAttributeName : label.font}];
}

+ (void)showHUDWithMessage:(NSString *)msg{
    if ([msg isEqualToString:@""]) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].delegate window] animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = [UIFont systemFontOfSize:18];
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1];
}

+ (BOOL)hasManyObject:(id)object {
    
    if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
        
        if ([object isKindOfClass:[NSNull class]] || (object == nil) ) {
            
            return NO;
            
        }else {
            
            if ([object count] > 1) {
                
                return YES;
                
            }else{
                
                return NO;
            }
        }
        
    }else {
        
        return NO;
    }
}

//****************
//判断字符串是否为空
+(BOOL)isNotNull:(id)object
{
    if ([object isEqual:[NSNull null]]) {
        return NO;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return NO;
        
    }else if (object == [NSNull null]){
        
        return NO;
        
    }else if (object==nil){
        
        return NO;
        
    }else if([object isKindOfClass:[NSArray class]]){
        if ([object count] == 0) {
            return NO;
        }
    }else if([object isKindOfClass:[NSDictionary class]]){
        if ([object count] ==0) {
            return NO;
        }
    }
    else {
        if ([object isKindOfClass:[NSString class]]) {
            NSString * str = [NSString trimWhitespaceAndNewline:object];
            if ([str isEqualToString:@""]) {
                return NO;
            }
            return YES;
        }
        return YES;
    }
    return YES;
}

//判断字符串是否为空
+(BOOL)isNull:(id)object
{
    if ([object isEqual:[NSNull null]]) {
        
        return YES;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else if (object == nil){
        return YES;
    }else if([object isKindOfClass:[NSArray class]]) {
        if ([object count] == 0) {
            return YES;
        }
    }else if ([object isKindOfClass:[NSDictionary class]]){
        if ([object count] == 0) {
            return YES;
        }
    }else {
        if ([object isKindOfClass:[NSString class]]) {
            
            NSString * str = [NSString trimWhitespaceAndNewline:object];
            if ([str isEqualToString:@""] || [object isEqualToString:@"(null)"]) {
                return YES;
            }
            return NO;
        }
        return NO;
    }
    return NO;
}


//获取Window当前显示的ViewController
+ (UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

+ (NSString *)UUID{
    NSString * currentDeviceUUID  = [[UIDevice currentDevice] identifierForVendor].UUIDString;
    
    currentDeviceUUID = [currentDeviceUUID stringByReplacingOccurrencesOfString:@"-" withString:@""];
    currentDeviceUUID = [currentDeviceUUID lowercaseString];
    
    return currentDeviceUUID;
}


@end
